<?php
/**
 * https://gitee.com/litephp
 * http://www.dazhetu.cn/
 * jay_fun 410136330@qq.com
 * Date: 2019/1/11
 * Time: 15:33
 */

namespace app\admin\controller\content;

use app\common\controller\BaseAdmin;
use app\common\model\content\User as UserModel;
use LiteAdmin\Music163;
use LiteAdmin\Tree;
use think\db;

/**
 * @title 用户管理
 * Class Music
 * @package app\admin\controller\content
 */
class User extends BaseAdmin
{
    /**
     * @title 列表页
     * @return mixed
     */
    public function index()
    {
        $db = UserModel::alias('u')
            ->join('content_balance b','b.uid = u.id','left')
			->join('content_user us','us.id = u.pid','left')
			->field('u.*,b.mineral,b.absenteeism,b.ontrial_absenteeism,b.profit,us.uname as usname')
            ->order('create_time desc');

        $search = $this->request->get();
        // 精准查询
        foreach (['status'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->where('r.'.$field,'=', $search[$field]);
            }
        }
        // 模糊查询
        foreach (['uname'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->whereLike('u.'.$field, "%{$search[$field]}%");
            }
        }
        return $this->_list($db, true, $search);
    }
    
	/**
     * @title 编辑操作
     * @return array|mixed
     */
	 public function edit()
     {
        return $this->_form(new UserModel(), 'form');
     }

	 protected function _form_before(&$data)
    {
        if($this->request->isPost()){
			//$data['create_time'] = $this->request->time();
			$balance = Db::name('ContentBalance')
				        ->where('uid',$data['id'])
				        ->update([
				                   'mineral'=>$data['mineral'],
				                   'absenteeism'=>$data['absenteeism'],
				                   'ontrial_absenteeism'=>$data['ontrial_absenteeism'],
				                   'profit'=>$data['profit'],
				        ]);
			$data2 = array('mineral'=>$data['mineral'],'absenteeism'=>$data['absenteeism'],'ontrial_absenteeism'=>$data['ontrial_absenteeism'],'profit'=>$data['profit']);
			$data = array_diff($data,$data2);
		}else{
			$list = UserModel::alias('u')
            ->join('content_balance b','b.uid = u.id','left')
			->join('content_user us','us.id = u.pid','left')
			->where('u.id',$data['id'])
			->field('u.*,b.mineral,b.absenteeism,b.ontrial_absenteeism,b.profit,us.uname as usname')->find();
			//dump($list);
            //$cates = Tree::array2list($list);
			//dump($cates);
            $this->assign('cates',$list);
		}
    }
	 
 /**
     * @title 审核
     */
    public function change()
    {
        $id = $this->request->post('id');
        $state = $this->request->post('status');

        $this->_change(new UserModel(), $id, ['status' => $state]);
    }
    
    /**
     * @title 删除
     */
    public function del()
    {
        $ids = $this->request->post('ids',false);
        !$ids && $this->error("缺少参数！");

        $this->_del(new UserModel(),$ids);
    }
}